#!/bin/bash
trap 'echo Abort ; exit 1' ERR
err() { echo "Error. $@ not set."; exit 1; }
. .varlist
[ -z "$SOURCE_DIR" ] && err SOURCE_DIR
[ -z "$BUILD_DIR" ] && err BUILD_DIR
[ -z "$BINUTILS" ] && err BINUTILS
[ -z "$HOST" ] && err HOST
[ -z "$TARGET" ] && err TARGET
[ -z "$TOOLCHAIN_DIR" ] && err TOOLCHAIN_DIR
[ -z "$STATIC_LIB_DIR" ] && err STATIC_LIB_DIR

echo Build binutils.
[ -f $TOOLCHAIN_DIR/.$BINUTILS ] && exit 0
rm $BUILD_DIR/$BINUTILS -rf
mkdir $BUILD_DIR/$BINUTILS
cd $BUILD_DIR/$BINUTILS
$SOURCE_DIR/$BINUTILS/configure --prefix=/usr --build=$HOST --host=$HOST \
	--target=$TARGET --with-sysroot=$TOOLCHAIN_DIR \
	--with-gmp=$STATIC_LIB_DIR --with-mpfr=$STATIC_LIB_DIR \
	--disable-nls --disable-werror --disable-multilib --with-float=soft > /dev/null
set -x
make > /dev/null
make DESTDIR=$TOOLCHAIN_DIR install > /dev/null
cp $SOURCE_DIR/$BINUTILS/include/libiberty.h $TOOLCHAIN_DIR/usr/include/
touch $TOOLCHAIN_DIR/.$BINUTILS
